//
//  LocationWithMapViewController.m
//  Unicorn
//
//  Created by zhupeng on 16/5/6.
//  Copyright © 2016年 bhj_jun. All rights reserved.
//

#import "LocationWithMapViewController.h"

@interface LocationWithMapViewController ()<MAMapViewDelegate,AMapSearchDelegate>
@property (nonatomic,retain)MAMapView *mapView;

@property (nonatomic,retain)AMapSearchAPI *search;

@property (nonatomic,assign)NSInteger modelNumber;

@end

@implementation LocationWithMapViewController

-(AMapSearchAPI *)search{
    if (!_search) {
        _search =[[AMapSearchAPI alloc] init];
    }
    return _search;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor redColor];
//    NSLog(@"%@,%@,%@",self.userLocation,self.userCity,self.lawyerDataArr);//测试
    self.modelNumber=0;
}
-(void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [MAMapServices sharedServices].apiKey = KmapAppKey;
    //    初始化地图 并且显示
    _mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
    _mapView.delegate = self;
    _mapView.showsUserLocation = YES;//YES 为打开定位，NO为关闭定位
    
    //    应为模拟器的默认地址为旧金山 所以不启用
        [_mapView setUserTrackingMode: MAUserTrackingModeFollow animated:YES]; //地图跟着位置移动
    [self.view addSubview:_mapView];

//    发起地理编码
    for (int i=0; i<self.lawyerDataArr.count; i++) {
        LawyerModel *model =[self.lawyerDataArr objectAtIndex:i];
        [self mapGeoCodeWithModel:model];
        
    }
    
}
//添加大头针
-(void)addAnnotationWithModel:(LawyerModel*)model addLocation:(AMapGeoPoint*)location{
    
    MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
    pointAnnotation.coordinate = CLLocationCoordinate2DMake(location.latitude,location.longitude);
    pointAnnotation.title = model.name;
    pointAnnotation.subtitle = model.addr;
    
    [_mapView addAnnotation:pointAnnotation];
}
//正向地理编码 //地名转经纬度
-(void)mapGeoCodeWithModel:(LawyerModel*)model{
    //配置用户Key
    [AMapSearchServices sharedServices].apiKey = KmapAppKey;
    //初始化检索对象
//    _search = [[AMapSearchAPI alloc] init];
    self.search.delegate = self;
    
    //构造AMapGeocodeSearchRequest对象，address为必选项，city为可选项
    AMapGeocodeSearchRequest *geo = [[AMapGeocodeSearchRequest alloc] init];
    
    
    geo.address = model.addr;
    
     //发起正向地理编码
        [_search AMapGeocodeSearch: geo];
    
  
}

//路径查询
-(void)mapRouteSearch{
      //构造AMapDrivingRouteSearchRequest对象，设置驾车路径规划请求参数
    AMapDrivingRouteSearchRequest *request = [[AMapDrivingRouteSearchRequest alloc] init];
    request.origin = [AMapGeoPoint locationWithLatitude:39.994949 longitude:116.447265];
    request.destination = [AMapGeoPoint locationWithLatitude:39.990459 longitude:116.481476];
    request.strategy = 2;//距离优先
    request.requireExtension = YES;
    
    //发起路径搜索
    [_search AMapDrivingRouteSearch: request];
    
    
    
}


#pragma mark - 地图代理方法
-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation
updatingLocation:(BOOL)updatingLocation
{
    if(updatingLocation)
    {
        //取出当前位置的坐标
        //        NSLog(@"latitude : %f,longitude: %f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
    }
}
//设置大头针样式
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
        MAPinAnnotationView*annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
        if (annotationView == nil)
        {
            annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
        }
        annotationView.canShowCallout= YES;       //设置气泡可以弹出，默认为NO
        annotationView.animatesDrop = YES;        //设置标注动画显示，默认为NO
        annotationView.draggable = YES;        //设置标注可以拖动，默认为NO
        annotationView.pinColor = MAPinAnnotationColorPurple;
        return annotationView;
    }
    return nil;
}

#pragma mark- 地图搜索功能回调
//实现正向地理编码的回调函数
- (void)onGeocodeSearchDone:(AMapGeocodeSearchRequest *)request response:(AMapGeocodeSearchResponse *)response
{
    if(response.geocodes.count == 0)
    {
        return;
    }
   
    for (AMapTip *p in response.geocodes) {

        NSLog(@"formattedDescription---%@,latitude--%g,longitude---%g",p.formattedDescription,p.location.latitude,p.location.longitude);
        
        [self addAnnotationWithModel:[self.lawyerDataArr objectAtIndex:self.modelNumber] addLocation:p.location];
        
    }


    self.modelNumber++;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
